import unittest
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By

class baidu(unittest.TestCase):
    def setUp(self) -> None:
        self.drivr = webdriver.Firefox()
        self.drivr.maximize_window()
        self.drivr.get('https://www.baidu.com/')
        sleep(5)


    def tearDown(self) -> None:
        sleep(10)
        self.drivr.quit()


    def test_baidu_01(self):
        # 点击照相机
        self.drivr.find_element(By.CLASS_NAME, 'soutu-btn').click()
        sleep(5)

        # 点击选择文件
        # 上传文件
        self.drivr.find_element(By.CLASS_NAME, 'upload-pic').send_keys(r'C:\Users\HP\Desktop\kw.png')
        sleep(5)

    def test_baidu_02(self):
        self.drivr.find_element(By.ID, 'kw').send_keys('Web端自动化')

    def test_baidu_03(self):
        self.drivr.find_element(By.LINK_TEXT, '贴吧').click()
        sleep(2)
        self.drivr.switch_to.window(self.drivr.window_handles[-1])
        sleep(2)
        # 找到目标元素
        element = self.drivr.find_element(By.XPATH, '//*[@id="f-d-w"]/div[4]/div/div[2]/a[2]')
        sleep(5)
        # 使用 JavaScript 滚动到元素
        self.drivr.execute_script("arguments[0].scrollIntoView();", element)




#执行
if __name__ == '__main__':
    unittest.main()